package com.servlets;

import com.domain.UserEntity;
import com.service.StudentService;
import com.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class UpdatePasswordServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        StudentService studentService = new StudentServiceImpl();

        req.setCharacterEncoding("utf-8");

        String oldPassword = req.getParameter("oldPassword");
        String password = req.getParameter("password");
        String repassword = req.getParameter("repassword");

        if(!password.equals(repassword)){
            resp.sendRedirect("loginError.jsp");
            return;
        }

        // 当前用户
        UserEntity user = (UserEntity) req.getSession().getAttribute("user");
        if(user==null){
            resp.sendRedirect("login.jsp");
        }else{
            if (user.getPwd().equals(oldPassword)) {
                // 更新用户

                resp.sendRedirect("success.jsp");
            }
        }

    }

}
